/****************************************************************************
 * Copyright (C) 2012 Dj_Skual
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************/
#include "Explorer.h"
#include "../Themes/CTheme.h"
#include "../Prompts/SearchWindow.h"
#include "../Controls/Application.h"
#include "../Tools/StartUpProcess.h"

#include "../Manage/Saves/SaveWindow.hpp"
#include "../Manage/Miis/MiiWindow.hpp"
#include "../Manage/Devices/DeviceWindow.hpp"

#undef CurrentBrowser
#undef CurrentGuiBrowser

Explorer *Explorer::instance = NULL;

Explorer::Explorer()
	: GuiFrame(0, 0)
	, switchingDisplay(false)
{
	if(Settings.StartUp)
		StartUpProcess::Instance()->SetTxt("Loading Explorer\n");
	
	trigB.SetButtonOnlyTrigger(-1, WiiControls.BackButton | ClassicControls.BackButton << 16, GCControls.BackButton);
	
	browserImgData = Resources::GetImageData("browser.png");
	browserImg = new GuiImage(browserImgData);
	
	parentDir = new GuiButton(0, 0);
	parentDir->SetTrigger(&trigB);
	parentDir->Clicked.connect(this, &Explorer::OnParentDirBtnClick);
	
	SetSize(browserImg->GetWidth(), browserImg->GetHeight());
	SetAlignment(thAlign("center - explorer align hor") | thAlign("top - explorer align ver"));
	SetPosition(thInt("0 - explorer pos x"), thInt("55 - explorer pos y"));
	
	pointed = new GuiPointed(width, height);
	pointed->StateChanged.connect(this, &Explorer::OnStateChange);
	
	fileBrowser = NULL;
	saveBrowser = NULL;
	miiBrowser = NULL;
	esaveBrowser = NULL;
	emiiBrowser = NULL;
	gcBrowser = NULL;
	
	guiBrowser = NULL;
	
	Reload();
}

Explorer::~Explorer()
{
	RemoveAll();
	
	delete guiBrowser;
	guiBrowser = NULL;
	
	if(fileBrowser)
		delete fileBrowser;
	if(saveBrowser)
		delete saveBrowser;
	if(miiBrowser)
		delete miiBrowser;
	if(esaveBrowser)
		delete esaveBrowser;
	if(emiiBrowser)
		delete emiiBrowser;
	if(gcBrowser)
		delete gcBrowser;
	fileBrowser = NULL;
	saveBrowser = NULL;
	miiBrowser = NULL;
	esaveBrowser = NULL;
	emiiBrowser = NULL;
	gcBrowser = NULL;
	
	Resources::Remove(browserImgData);
	delete browserImg;
	
	delete parentDir;
	delete pointed;
}

void Explorer::show()
{
	SetEffect(EFFECT_FADE, 20);
	Application::Instance()->Append(this);
}

void Explorer::hide()
{
	SetEffect(EFFECT_FADE, -20);
}

void Explorer::Reload()
{
	RemoveAll();
	
	Append(browserImg);
	Append(parentDir);
	Append(pointed);
	
	if(guiBrowser)
		delete guiBrowser;
	guiBrowser = NULL;
	
	if(!switchingDisplay)
	{
		curBrowser = NULL;
		
		if(fileBrowser)
			delete fileBrowser;
		if(saveBrowser)
			delete saveBrowser;
		if(miiBrowser)
			delete miiBrowser;
		if(esaveBrowser)
			delete esaveBrowser;
		if(emiiBrowser)
			delete emiiBrowser;
		if(gcBrowser)
			delete gcBrowser;
		fileBrowser = NULL;
		saveBrowser = NULL;
		miiBrowser = NULL;
		esaveBrowser = NULL;
		emiiBrowser = NULL;
		gcBrowser = NULL;
	}
	
	if(Settings.LoadedBrowser == SDUSB_BROWSER)
	{
		if(!switchingDisplay)
		{
			fileBrowser = new FileBrowser();
			fileBrowser->BrowsePath(Settings.LastUsedPath);
			curBrowser = fileBrowser;
		}
		
		if(Settings.BrowserDisplayMode == ICON_DISPLAY)
			guiBrowser = new IconFileBrowser(curBrowser, width, height);
		else
			guiBrowser = new ListFileBrowser(curBrowser, width, height);
		
		guiBrowser->Clicked.connect(this, &Explorer::OnDeviceBrowserClick);
	}
	else if(Settings.LoadedBrowser == SAVE_BROWSER ||
			Settings.LoadedBrowser == ESAVE_BROWSER)
	{
		if(!switchingDisplay)
		{
			if(Settings.LoadedBrowser == SAVE_BROWSER)
			{
				saveBrowser = new SaveBrowser();
				curBrowser = saveBrowser;
			}
			else
			{
				esaveBrowser = new ESaveBrowser();
				esaveBrowser->GetList(Settings.EmuUsedPath);
				curBrowser = esaveBrowser;
			}
		}
		
		if(Settings.BrowserDisplayMode == ICON_DISPLAY)
			guiBrowser = new IconSaveBrowser(curBrowser, width, height);
		else
			guiBrowser = new ListSaveBrowser(curBrowser, width, height);
		
		guiBrowser->Clicked.connect(this, &Explorer::OnSaveBrowserClick);
	}
	else if(Settings.LoadedBrowser == MII_BROWSER ||
			Settings.LoadedBrowser == EMII_BROWSER)
	{
		if(!switchingDisplay)
		{
			if(Settings.LoadedBrowser == MII_BROWSER)
			{
				miiBrowser = new MiiBrowser();
				curBrowser = miiBrowser;
			}
			else
			{
				emiiBrowser = new EMiiBrowser();
				emiiBrowser->GetList(Settings.EmuUsedPath);
				curBrowser = emiiBrowser;
			}
		}
		
		if(Settings.BrowserDisplayMode == ICON_DISPLAY)
			guiBrowser = new IconMiiBrowser(curBrowser, width, height);
		else
			guiBrowser = new ListMiiBrowser(curBrowser, width, height);
		
		guiBrowser->Clicked.connect(this, &Explorer::OnMiiBrowserClick);
	}
	else if(Settings.LoadedBrowser == GC_BROWSER)
	{
		if(!switchingDisplay)
		{
			gcBrowser = new GCBrowser();
			curBrowser = gcBrowser;
		}
		
		if(Settings.BrowserDisplayMode == ICON_DISPLAY)
			guiBrowser = new IconGCBrowser(curBrowser, width, height);
		else
			guiBrowser = new ListGCBrowser(curBrowser, width, height);
		
		guiBrowser->Clicked.connect(this, &Explorer::OnGCBrowserClick);
	}
	
	if(switchingDisplay)
	{
		curBrowser->SetSelectedIndex(0);
		curBrowser->IndexChanged();	
		switchingDisplay = false;
	}
	
	if(guiBrowser)
		Append(guiBrowser);
	
	guiBrowser->SetVisible(true);
}

void Explorer::OnStateChange(GuiElement *sender UNUSED, int State, int stateChan UNUSED)
{
	if(State == STATE_SELECTED)
	{
		Application::Instance()->BringToFront(this);
		Application::Instance()->BringToFront(SearchWindow::Instance());
	}
}

void Explorer::OnParentDirBtnClick(GuiButton *sender UNUSED, int pointer UNUSED, const POINT &p UNUSED)
{
	Application::Instance()->ResetPads();
	
	if(Settings.LoadedBrowser == SDUSB_BROWSER)
		curBrowser->BackInDirectory();
}

void Explorer::OnDeviceBrowserClick(int index UNUSED)
{
	if(curBrowser->IsCurrentDir())
	{
		if(curBrowser->ChangeDirectory() < 0)
			ShowError(tr("Can't browse that path."));
	}
	else if(!SearchWindow::Instance()->IsRunning())
	{
		if (curBrowser->IsMii(curBrowser->GetSelIndex()) ||
			curBrowser->IsSave(curBrowser->GetSelIndex()) ||
			curBrowser->IsDataBin(curBrowser->GetSelIndex()))
		{
			DeviceWindow * manageWindow = new DeviceWindow();
			manageWindow->Closing.connect(this, &Explorer::OnManageWindowClose);
		}
		else
			gprintf("\n\t file clicked");
	}
}

void Explorer::OnSaveBrowserClick(int index UNUSED)
{
	SaveWindow * manageWindow = new SaveWindow();
	manageWindow->Closing.connect(this, &Explorer::OnManageWindowClose);
}

void Explorer::OnMiiBrowserClick(int index UNUSED)
{
	MiiWindow * manageWindow = new MiiWindow();
	manageWindow->Closing.connect(this, &Explorer::OnManageWindowClose);
}

void Explorer::OnGCBrowserClick(int index)
{
	gprintf("\n\t GC(%i) clicked", index);
}

/*void Explorer::OnManageWindowClose(GuiFrame * manageWindow UNUSED)
{
	gprintf("\n\t OnManageWindowClose");
}*/
